//
//  ByShopDetailProductDescCell.m
//  hutouben_ios
//
//  Created by pro on 16/11/30.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByShopDetailProductDescCell.h"

#pragma mark - layout
@implementation ByShopDetailProductDescCellLayout

- (void)setModel:(ByNetProductDetailModel *)model {
    
    _model = model;
    
    CGFloat x, y, width, height;
    
    // 1.介绍
    x       = 10;
    y       = 10;
    width   = SCREEN_WIDTH - x * 2;
    height  = [model.desc getRectOfAttributeStringWithSize:CGSizeMake(width, 1000) font:14].size.height;
    height  = MAX(height, 20);
    self.descFrame = CGRectMake(x, y, width, height);
    
    // 2.线条1
    x       = 10;
    y       = CGRectGetMaxY(self.descFrame) + 10;
    width   = SCREEN_WIDTH - x * 2;
    height  = 0.5;
    self.line1Frame = CGRectMake(x, y, width, height);
    
    // 3.邮费
    x       = 10;
    y       = CGRectGetMaxY(self.line1Frame) + 10;
    width   = SCREEN_WIDTH - x * 2;
    height  = 20;
    self.postageFrame = CGRectMake(x, y, width, height);
    
    // 4.线条2
    x       = 10;
    y       = CGRectGetMaxY(self.postageFrame) + 10;
    width   = SCREEN_WIDTH - x * 2;
    height  = 0.5;
    self.line2Frame = CGRectMake(x, y, width, height);
    
    // 5.参数
    x       = 10;
    y       = CGRectGetMaxY(self.line2Frame) + 10;
    width   = SCREEN_WIDTH - x * 2;
    height  = model.params.count * 20 + (model.params.count - 1) * 5;
    self.paramsFrame = CGRectMake(x, y, width, height);
    
    // 6.cell
    x       = 0;
    y       = 0;
    width   = SCREEN_WIDTH;
    height  = CGRectGetMaxY(self.paramsFrame) + 10;
    self.cellFrame = CGRectMake(x, y, width, height);
}

@end


@implementation ByShopDetailProductDescCell
{
    UILabel *_descLabel;            // 标题
    UILabel *_line1;                // 线
    UILabel *_postageLabel;         // 线
    UILabel *_line2;                // 线
    UIView  *_paramsView;           // 价格
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    // 介绍
    _descLabel = [[UILabel alloc] init];
    _descLabel.adjustsFontSizeToFitWidth = YES;
    _descLabel.numberOfLines = 0;
    _descLabel.font = FONT(14);
    _descLabel.textColor = [UIColor grayColor];
    _descLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_descLabel];
    
    // 线条1
    _line1 = [[UILabel alloc] init];
    _line1.backgroundColor = ZJ_SEPLINE_COLOR;
    [self.contentView addSubview:_line1];
    
    // 邮费
    _postageLabel = [[UILabel alloc] init];
    _postageLabel.adjustsFontSizeToFitWidth = YES;
    _postageLabel.font = FONT(14);
    _postageLabel.textColor = HTB_THEME_COLOR;
    _postageLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_postageLabel];
    
    // 线
    _line2 = [[UILabel alloc] init];
    _line2.backgroundColor = ZJ_SEPLINE_COLOR;
    [self.contentView addSubview:_line2];
    
    // 参数
    _paramsView = [[UIView alloc] init];
    [self.contentView addSubview:_paramsView];
}
- (void)setLayout:(ByShopDetailProductDescCellLayout *)layout {
    
    _layout = layout;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    ByNetProductDetailModel *model  = _layout.model;
    
    // 设置布局
    _descLabel.frame                = _layout.descFrame;
    _line1.frame                    = _layout.line1Frame;
    _postageLabel.frame             = _layout.postageFrame;
    _line2.frame                    = _layout.line2Frame;
    _paramsView.frame               = _layout.paramsFrame;

    // 设置属性
    _descLabel.attributedText       = [model.desc attributeString:14];
    _postageLabel.text              = [NSString stringWithFormat:@"邮费:￥%@", model.postage];
    
   
    [self setupParamsView:model];
}

- (void)setupParamsView:(ByNetProductDetailModel *)model {
    
    [_paramsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat x, y, width, height;
    CGFloat margin = 5;
    for (int i = 0; i < model.params.count; i ++) {
        width = _paramsView.width;
        height = 20;
        x = 0;
        y = i * (margin + height);
        UIView *basalView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [_paramsView addSubview:basalView];
        
        x = 0;
        y = 0;
        width = 5;
        height = 5;
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        imgView.backgroundColor = HTB_THEME_COLOR;
        [basalView addSubview:imgView];
        imgView.y = basalView.height / 2;
        
        x = imgView.right + 10;
        y = 0;
        width = basalView.width - x;
        height = basalView.height;
        UILabel *paramLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        paramLabel.adjustsFontSizeToFitWidth = YES;
        paramLabel.font = FONT(13);
        paramLabel.text = @"产品参数：参数描述";
        paramLabel.textColor = [UIColor grayColor];
        paramLabel.textAlignment = NSTextAlignmentLeft;
        [basalView addSubview:paramLabel];
    }
}

@end
